package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.FindCard;
import reduce.fz.com.reduce.bean.TeamCategory;
import reduce.fz.com.reduce.custom.HeaderFooterStatusRecyclerViewAdapter;

/**
 * Created by qingshengzheng on 15/7/7.
 */
public class FindCoverAdapter extends HeaderFooterStatusRecyclerViewAdapter<FindCoverAdapter.CoverViewHolder> {
    private TeamCategory findFolder;
    private List<FindCard> findCards;
    private Context context;
    private LayoutInflater mLayoutInflater;

    public FindCoverAdapter(Context context, TeamCategory findFolder, List<FindCard> findCards) {
        this.context = context;
        this.findFolder = findFolder;
        this.findCards = findCards;
        this.mLayoutInflater = LayoutInflater.from(context);
    }


    @Override
    public CoverViewHolder createFooterStatusViewHolder(View footerView) {
        return new ContentViewHolder(footerView, this);
    }

    @Override
    protected int getHeaderItemCount() {
        return 1;
    }

    @Override
    protected int getContentItemCount() {
        if (findCards.size() % 2 == 0)
            return findCards.size() / 2;
        else return findCards.size() / 2 + 1;
    }

    @Override
    protected CoverViewHolder onCreateHeaderItemViewHolder(ViewGroup parent, int headerViewType) {
        View v = mLayoutInflater.inflate(R.layout.layout_findcover_head_item, parent, false);
        CoverViewHolder h = new HeaderViewHolder(v, this);
        return h;
    }

    @Override
    protected CoverViewHolder onCreateContentItemViewHolder(ViewGroup parent, int contentViewType) {
        View v = mLayoutInflater.inflate(R.layout.layout_findcover_content_item, parent, false);
        ContentViewHolder h = new ContentViewHolder(v, this);
        return h;
    }

    @Override
    protected void onBindHeaderItemViewHolder(CoverViewHolder headerViewHolder, int position) {
        ((HeaderViewHolder) headerViewHolder).renderView(findFolder);
    }

    @Override
    protected void onBindContentItemViewHolder(CoverViewHolder contentViewHolder, int position) {
        FindCard card1 = findCards.get(position * 2);
        FindCard card2 = position * 2 + 1 > findCards.size() ? null : findCards.get(position * 2 + 1);
        ((ContentViewHolder) contentViewHolder).renderView(card1, card2);
    }


    public static class ContentViewHolder extends CoverViewHolder {
        private ImageView mFindCoverContentImage1;
        private TextView mFindCoverContentAddress1;
        private ImageView mFindCoverContentImage2;
        private TextView mFindCoverContentAddress2;

        public ContentViewHolder(View itemView, FindCoverAdapter adapter) {
            super(itemView, adapter);
            mFindCoverContentImage1 = (ImageView) itemView.findViewById(R.id.find_cover_content_image1);
            mFindCoverContentAddress1 = (TextView) itemView.findViewById(R.id.find_cover_content_address1);
            mFindCoverContentImage2 = (ImageView) itemView.findViewById(R.id.find_cover_content_image2);
            mFindCoverContentAddress2 = (TextView) itemView.findViewById(R.id.find_cover_content_address2);
        }

        public void renderView(FindCard card1, FindCard card2) {
            mFindCoverContentImage1.setImageURI(null);
            mFindCoverContentImage2.setImageURI(null);
            mFindCoverContentAddress1.setText(card1.getAddress());
            mFindCoverContentAddress2.setText(card2.getAddress());


        }
    }

    /**
     *
     */
    public static class HeaderViewHolder extends CoverViewHolder {
        private ImageView mFindCoverImage;
        private TextView mFindCoverAddress;

        public HeaderViewHolder(View itemView, FindCoverAdapter adapter) {
            super(itemView, adapter);
            mFindCoverImage = (ImageView) itemView.findViewById(R.id.find_cover_image);
            mFindCoverAddress = (TextView) itemView.findViewById(R.id.find_cover_address);
        }

        public void renderView(Object obj) {
            TeamCategory folder = (TeamCategory) obj;
            mFindCoverImage.setImageURI(null);
           // mFindCoverAddress.setText((folder.getCitys()));
        }
    }

    public abstract static class CoverViewHolder extends RecyclerView.ViewHolder {
        private FindCoverAdapter madapter;

        public CoverViewHolder(View itemView, FindCoverAdapter adapter) {
            super(itemView);
            this.madapter = adapter;
        }

    }

}
